/********************************************************************
  created:  2007/06/11
  created:  11:6:2007   16:36
  filename:   d:\WZX\zzz\Render\Render\WZX\Render\Script\Script.hpp
  file path:  d:\WZX\zzz\Render\Render\WZX\Render\Script
  file base:  Script
  file ext:  hpp
  author:    WZX
  
  purpose:  zzz script load base class. should be used to load zzz script only,
            since it removed comments and ignored letter cases which may not compatible with other script.
*********************************************************************/
#pragma once

#include <common.hpp>
#include <Utility/Tools.hpp>
#include <Utility/FileTools.hpp>

namespace zzz{
template <class T>class Script
{
public:
  static bool LoadFromMemory(istringstream &mem, T &ret, const string &path);

  static inline bool LoadFromMemory(const char *mem, T &ret, const string &path);
  static inline bool LoadFromMemory(const string &mem, T &ret, const string &path);
  static inline T* LoadFromMemory(const char *mem, const string &path);
  static inline T* LoadFromMemory(const string &mem, const string &path);
  static inline T* LoadFromMemory(const istringstream &mem, const string &path);

  static inline bool LoadFromFile(const char *filename, T &ret);
  static inline T* LoadFromFile(const char *filename);

  string m_path;
};

template <class T>
T* zzz::Script<T>::LoadFromFile( const char *filename )
{
  T *ret=new T;
  if (LoadFromFile(filename,*ret))
    return ret;
  delete ret;
  return 0;
}
template <class T>
bool zzz::Script<T>::LoadFromFile( const char *filename, T &ret )
{
  printf("Load script: %s\n",filename);
  string m_path=filename;
  m_path=GetPath(m_path);
  string str;
  ReadFileToString(filename,str);
  return LoadFromMemory(str,ret,m_path);
}
template <class T>
T* zzz::Script<T>::LoadFromMemory( const istringstream &mem, const string &path )
{
  T* ret=new T;
  if (LoadFromMemory(mem,*ret,path))
    return ret;
  delete ret;
  return 0;
}
template <class T>
T* zzz::Script<T>::LoadFromMemory( const string &mem, const string &path )
{
  T* ret=new T;
  if (LoadFromMemory(mem,*ret,path))
    return ret;
  delete ret;
  return 0;
}
template <class T>
T* zzz::Script<T>::LoadFromMemory( const char *mem, const string &path )
{
  T* ret=new T;
  if (LoadFromMemory(mem,*ret,path))
    return ret;
  delete ret;
  return 0;
}
template <class T>
bool zzz::Script<T>::LoadFromMemory( const string &mem, T &ret, const string &path )
{
  istringstream iss(RemoveComments_copy(mem));
  return LoadFromMemory(iss,ret,path);
}
template <class T>
bool zzz::Script<T>::LoadFromMemory( const char *mem, T &ret, const string &path )
{
  string str=mem;
  return LoadFromMemory(str,ret,path);
}
}
